<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cjenis_hardware extends DR_Controller {

	function __Construct()
	{
		parent::__Construct();
		$this->load->model("jenis_alat");
		$this->load->library("pagination");		
		
		$this->only_admin("admin/main/login");
	}
	
	private function redirect_if_null($id=null,$url=null)
	{
		if($id==null)
		{
			if($url!=null)
			{
				redirect($url);
			}
			
			$this->redirect_to_index();
		}
	}
	
	private function redirect_to_index()
	{
		redirect("admin/cjenis_hardware");
	}
		
	public function add_property($id_jenishardware=null)
	{
		if($id_jenishardware!=null)
		{
			$nama = $this->read_post("nama_property");
			$keterangan = $this->read_post("keterangan");
			
			$data = array("id_jenisalat"=>$id_jenishardware,"nama_property"=>$nama,"keterangan"=>$keterangan);
			$this->jenis_alat->properties->save($data);
			redirect("admin/cjenis_hardware/properties/$id_jenishardware");
		}
		else
		{
			$this->redirect_to_index();
		}
	}
	
	/**
	* dialog add
	*/
	function add()
	{
		$data = array();
		$data["form_caption"] = "Jenis Alat";
		$data["add_info"] = $this->native_session->flashdata("SAVE_MESSAGE");
		$data["btn_caption"] = "Simpan";
		$data["action"] = "admin/cjenis_hardware/save";
		$data["alat"] = $this->jenis_alat->get_blank_object();
		$this->set_view("admin/header",'admin/jenis_alat/addnew',$data);
	}
	
	function save()
	{
		$nama = $this->read_post("nama");
		$kontainer = $this->read_post("kontainer");
		$kontainer = ($kontainer!="Y"?"N":"Y");
		
		$id_jenis = (int) $this->read_post("id_jenisalat");
		if(strlen($nama)>0){
			$data = array("nama_jenisalat"=>$nama,"is_kontainer"=>$kontainer);
			//update
			if($id_jenis>0)
			{
				$this->jenis_alat->update($data,$id_jenis);
				redirect("admin/cjenis_hardware");			
			}
			else
			{
				//add
				$this->jenis_alat->save($data);
				$this->native_session->set_flashdata("SAVE_MESSAGE", "$nama tersimpan");
				redirect("admin/cjenis_hardware/add");
			}
		}else{
			redirect("admin/cjenis_hardware");
		}	
	}
	
	function hapus_properties($id_jenis,$id_property)
	{
		$this->jenis_alat->properties->delete($id_property);
		redirect("admin/cjenis_hardware/properties/$id_jenis");
	}
	
	function properties($id_jenis=null)
	{
		$data = array();
		if($id_jenis!=null)
		{
			$current = $this->jenis_alat->get_by_id($id_jenis);
			$data["current"] = $current;
			$data["properties"] = $this->jenis_alat->properties->get_by_jenisalat($id_jenis);
			$data["id_jenishardware"] = $id_jenis;
			$this->set_view("admin/header",'admin/jenis_alat/properties',$data);
		}
		else
		{
			$this->redirect_to_index();
		}
	}
	
	function index($offset=null,$field=null,$value=null)
	{
		$filter=null;
		if($field!=null & $value!=null){
			$offset = $this->uri->segment(7); 
			$config['uri_segment']=7;					
			$filter = "$field like '%$value%'";
			$config['base_url'] = site_url("/admin/cjenis_alat/index/0/$field/$value");
			$config['total_rows'] = $this->jenis_alat->get_count($filter);			
			
			$data["filtered"] = true;
			
			//download filtered
			$this->session->set_flashdata("filter",$filter);			
			
		}
		else
		{
			$data["filtered"] = false;
			
			$offset = $this->uri->segment(4); 
			$config['uri_segment']=4;					
			$filter = array();
			$config['base_url'] = site_url('/admin/jenis_alat/index/');
			$config['total_rows'] = $this->jenis_alat->get_count($filter);			
		}
				
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();
		
		$data["offset"] = $offset;
		$this->jenis_alat->set_default_order(array("nama_jenisalat"=>"asc"));		
		$data['result'] = $this->jenis_alat->get_paged($config['per_page'],$offset,$filter);
		$data['total_page'] = $paginator;		
		$data["config"] = $config;
		
		//sekarang gunakan set view. agar content dirender didalam container
		$this->set_view("admin/header",'admin/jenis_alat/list',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */